package com.payment.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.payment.common.base.AjaxResult;
import com.payment.common.enums.SystemConfigCode;
import com.payment.common.support.Convert;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.AppVersion;
import com.payment.system.domain.SystemConfig;
import com.payment.system.mapper.AppVersionMapper;
import com.payment.system.mapper.SystemConfigMapper;
import com.payment.system.service.IAppVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * app版本 服务层实现
 *
 * @author payment
 * @date 2019-01-14
 */
@Service
public class AppVersionServiceImpl implements IAppVersionService {
    @Autowired
    private AppVersionMapper appVersionMapper;

    @Autowired
    private SystemConfigMapper configMapper;

    /**
     * 查询app版本信息
     *
     * @param id app版本ID
     * @return app版本信息
     */
    @Override
    public AppVersion selectAppVersionById(String id) {
        return appVersionMapper.selectAppVersionById(id);
    }

    /**
     * 查询app版本列表
     *
     * @param appVersion app版本信息
     * @return app版本集合
     */
    @Override
    public List<AppVersion> selectAppVersionList(AppVersion appVersion) {
        return appVersionMapper.selectAppVersionList(appVersion);
    }

    /**
     * 新增app版本
     *
     * @param appVersion app版本信息
     * @return 结果
     */
    @Override
    public int insertAppVersion(AppVersion appVersion) {
        return appVersionMapper.insertAppVersion(appVersion);
    }

    /**
     * 修改app版本
     *
     * @param appVersion app版本信息
     * @return 结果
     */
    @Override
    public int updateAppVersion(AppVersion appVersion) {
        return appVersionMapper.updateAppVersion(appVersion);
    }

    /**
     * 删除app版本对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAppVersionByIds(String ids) {
        return appVersionMapper.deleteAppVersionByIds(Convert.toStrArray(ids));
    }

    @Override
    public AjaxResult updateStatusById(String id, String status) {
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)) {
            return AjaxResult.error("获取信息失败：缺失参数！");
        }
        int r = appVersionMapper.updateStatusById(id, status);
        return r > 0 ? AjaxResult.success() : AjaxResult.error("修改状态失败，请联系后台管理人员！");
    }


    /**
     * 获取当前最新的版本 0 ios,1 安卓
     *
     * @param type
     * @return
     */
    @Override
    public AjaxResult getVersion(String type, String versionNo) {
        if (StringUtils.isEmpty(type) || StringUtils.isEmpty(versionNo)) {
            return AjaxResult.error(1, "缺失参数");
        }
        if (type.equals("0") && versionNo.equals("1.0.2")){
            return AjaxResult.error(3, "暂无可更新的版本");
        }
        AppVersion appVersion = appVersionMapper.selectAppVersionByNow(type);
        if (appVersion != null) {
            Map<String, Object> map = new HashMap<>(16);
            if (!StringUtils.equals(appVersion.getVersionNo(), versionNo)) {
                map.put("systemType", appVersion.getSystemType());
                map.put("versionNo", appVersion.getVersionNo());
                map.put("programDescription", appVersion.getProgramDescription());

                if (appVersion.getSystemType().equals("0")) {
                    map.put("linkUrl", "https://apps.apple.com/cn/app/178t%E6%97%B6%E4%BB%A3/id1501633879");//连接下载地址11
                } else {
                    map.put("linkUrl", "https://pay-app.oss-cn-beijing.aliyuncs.com/5Gzhinengshidai.apk");//连接下载地址
                }
                return AjaxResult.successAndData("版本号不一致需要更新", map);
            }
            return AjaxResult.error(3, "暂无可更新的版本");

        } else {
            return AjaxResult.error(2, "获取最新版本号失败");
        }


    }

    /**
     * 获取手机端上架信息
     *
     * @param systemType
     * @return
     */
    @Override
    public AjaxResult getIsOnline(String systemType) {
        if (StringUtils.isEmpty(systemType)) {
            return AjaxResult.error(1, "手机系统类型不能为空！");
        }
        Map<String, String> map = new HashMap<>(2);
        SystemConfig systemConfig = configMapper.selectSystemConfigByConfigCode(SystemConfigCode.IS_ONLINE.getCode());
        //苹果0  1安卓
        JSONObject isOnlineJson = JSONObject.parseObject(systemConfig.getConfigValue());
        if (systemType.equals("0")) {
            map.put("isOnline", isOnlineJson.getString("ios"));
        } else if (systemType.equals("1")) {
            map.put("isOnline", isOnlineJson.getString("android"));
        } else {
            return AjaxResult.error(4, "手机版本类型不存在");
        }

        return AjaxResult.successAndData("获取成功", map);
    }

}
